
#include <BSize>
#include <BFont>
#include <BWidget>
#include <BStyle>

#include "member_BButton.h"

using namespace BWE;

member_BButton::member_BButton(BButton* button)
{
	boss = button;
	iconAlone = false;
	checkable = false;
	checked = false;
	state = State_Normal;
	align = Align_Center;
	color.set(0, 0, 0, 0);
	dirty = false;
	space = 0;
}
member_BButton::~member_BButton()
{

}

void member_BButton::freshRects()
{
	const BStyle* style = boss->realStyle();
	if (style)
		style->trans(text, t_text);
	else
		t_text.clear();

	const BFont* font = boss->font();
	if (font)
	{
		if (t_text.size())
			textSize = font->textSize(t_text);
		else
			textSize = font->textSize(text);
	}

	BRect crect = boss->clientRect();

	int cx = crect.x();
	int cy = crect.y();
	int cw = crect.width();
	int ch = crect.height();

	realIconSize.set(0, 0);
	int width = space + textSize.width();
	if (icon)
	{
		realIconSize = iconSize;
		if (iconSize.width() <= 0)
			realIconSize.width() = textSize.height();
		if (iconSize.height() <= 0)
			realIconSize.height() = textSize.height();
		realIconSize.wrap(crect.size());
		width += realIconSize.width();
	}

	if (width > crect.width())
		width = crect.width();
	BRect area = crect.align(align, width, crect.height());

	BRect textArea = area;
	BRect iconArea = area;
	bool showText = !(icon && iconAlone);
	if (showText && icon && text.size())
	{
		iconArea.setWidth(realIconSize.width());
		textArea.left() = iconArea.right() + space;
	}
	iconRect = iconArea.align(Align_Center, realIconSize);
	textRect = textArea.align(Align_Left | Align_VCenter, area.width() - realIconSize.width(), textSize.height());

	if (image)
	{
		BSize realImageSize = imageSize;
		if (imageSize.width() <= 0)
			realImageSize.width() = image->width();
		if (imageSize.height() <= 0)
			realImageSize.height() = image->height();
		realImageSize.wrap(crect.size());
		imageRect = crect.align(Align_Center, realImageSize);
	}
}
